package com.hzlx.service;

import com.hzlx.bean.Permission;
import com.hzlx.dao.PermissionDao;
import com.hzlx.dto.MenuDto;
import com.hzlx.dto.MenuItemDto;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PermissionService {
    @Resource
    PermissionDao permissionDao;
    public List<MenuDto> findMenus(String account){
        List<Permission> menus =permissionDao.findAllRoutesByUser(account);
     return menus.stream().filter(perm->perm.getPermType()==0).distinct().map(perm-> {
           MenuDto menuDto= MenuDto.builder()
                    .id(perm.getId())
                    .title(perm.getShowName())
                    .build();
           //下一级
              List<MenuItemDto> subs =  menus.stream().filter(p->p.getParentId() == menuDto.getId()).distinct()
                 .map(p->MenuItemDto.builder()
                         .page(p.getPage())
                         .title(p.getShowName())
                         .build()).collect(Collectors.toList());
         menuDto.setMenuItem(subs);
            return menuDto;
        }).collect(Collectors.toList());

    }
}
